Repository navigation
Conversation
sign_in stores a UUID user id in the JWT as a string, but get_user_id_from_session_id returned it unchanged and passed it to get_obj. SQLAlchemy's Uuid type calls .hex on the bound value, so loading the signed-in user for a model with a UUID primary key raised AttributeError and /api/me and /api/configuration returned 500. Parse a string user id back into a UUID before the lookup, and treat a value that is not a valid UUID as an invalid session. Integer user ids are unaffected.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
References
Fixes #144
Summary
ApiService.sign_instores a UUID user id in the JWT as a string.get_user_id_from_session_idthen returned that string unchanged and passed it toadmin_model.get_obj. SQLAlchemy'sUuidtype calls.hexon the bound parameter, so for a model with a UUID primary key (for example on SQLite) the lookup raisedAttributeError: 'str' object has no attribute 'hex', and/api/meand/api/configurationreturned 500 right after a successful sign-in.The function is annotated
-> UUID | int | None, so this change makes it honour that: a string user id from the token is parsed back into aUUIDbefore the lookup. A string that is not a valid UUID is treated as an invalid session and returnsNone. Integer user ids are unchanged. Onlyint | UUIDids can be issued bysign_in, so a string id in a token always came from a UUID.Tests added in
tests/api/test_service.py:sign_inandget_user_id_from_session_idas aUUID, andget_objreceives theUUIDNonewithout callingget_objUuidprimary key on in-memory SQLite can load the signed-in user (the case from the issue)Before the change, the UUID, malformed-id and SQLAlchemy tests fail (the latter with the
.hexerror from the issue). After it, they pass.Checked locally:
pytest -n auto tests(2110 passed),ruff check,ruff format --check,ty check fastadmin.Are there any open tasks or blockers?
None.